package top.infopub.job.cap.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.enums.cap.CapProcessDetailTypeEnum;
import top.infopub.job.cap.dao.RepaymentScheduleDao;
import top.infopub.job.cap.domain.RepayScheduleDomain;
import top.infopub.job.cap.service.RepaymentScheduleService;
import top.infopub.job.flow.service.OAFlowService;
import top.infopub.model.cap.FinanceReimbursement;
import top.infopub.model.cap.FinanceRepaySub;
import top.infopub.model.cap.LoanContract;
import top.infopub.model.cap.LoanFin;
import top.infopub.util.IdFactory;
import top.infopub.util.UtilTools;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: Tszzzzzz
 * @Date: 2021/6/7
 */
@Service
public class RepaymentScheduleServiceImpl implements RepaymentScheduleService {

    @Autowired
    private RepaymentScheduleDao repaymentScheduleDao;
    @Autowired
    private OAFlowService oaFlowService;

    private static String TABLE_NAME = "CAP_FINANCE_REIMBURSEMENT";

    /**
     * 还款计划中 还本日期到当天日期的生成还款列表数据并发送代办
     **/
    @Override
    @Transactional
    public void autoGenerateRepay() throws Exception {
        //查询所有审批通过的还款计划--当天
        List<RepayScheduleDomain> schedules = repaymentScheduleDao.queryStayRepayList();
        if (schedules != null && schedules.size() > 0) {
            //查询当前计划下的
            for (RepayScheduleDomain schedule : schedules) {
                //一个还款金额对应一条待办 对应一条还款数据
                int count = repaymentScheduleDao.queryFinanceReimbCount(schedule.getProCode(), schedule.getSetupFinancialId());
                if (count > 0) continue;
                //查询放款主表信息
                List<LoanFin> loanFinList = repaymentScheduleDao.selectLoanFin(schedule.getProCode());
                //没有对应放款记录不做还款
                if (loanFinList == null || loanFinList.size() <= 0) continue;
                //已还金额
                BigDecimal paidAmt = repaymentScheduleDao.querySumRepayAmt(schedule.getProCode(), schedule.getSetupFinancialId());
                if (paidAmt == null) paidAmt = new BigDecimal(0);
                LoanFin loanFin = loanFinList.get(0);
                FinanceReimbursement reimbursement = new FinanceReimbursement();
                reimbursement.setId(IdFactory.getUUIDUpper());
                reimbursement.setCodes(IdFactory.getUUIDUpper());
                reimbursement.setProCode(schedule.getProCode());
                reimbursement.setAddBy(schedule.getAddBy());
                reimbursement.setAddTime(new Date());
                reimbursement.setUptBy(schedule.getAddBy());
                reimbursement.setUptTime(new Date());
                reimbursement.setRepayAmt(schedule.getRepayAmt());
                reimbursement.setSetupFinancialId(schedule.getSetupFinancialId());
                //查询借款合同ID
                List<LoanContract> loanContracts = repaymentScheduleDao.selectLoanContract(schedule.getProCode());
                if (loanContracts.size() > 0) {
                    reimbursement.setLoanContId(loanContracts.get(0).getId());
                }
                //发送代办
                String oaTitle = UtilTools.getCapOaTitle(schedule.getProName(), CapProcessDetailTypeEnum.PRO_NODE_TYPE17);
                String oaId = oaFlowService.sendOAToDo(oaTitle, schedule.getAddBy(), schedule.getAddBy(), TABLE_NAME, reimbursement.getId(), schedule.getProCode());
                reimbursement.setOaId(oaId);
                repaymentScheduleDao.insertSement(reimbursement);
                //子表数据
                FinanceRepaySub sub = new FinanceRepaySub();
                sub.setAddBy(schedule.getAddBy());
                sub.setAddTime(new Date());
                sub.setUptBy(schedule.getAddBy());
                sub.setUptTime(new Date());
                sub.setFinRepayId(reimbursement.getId());
                sub.setLoanFinId(loanFin.getId());
                sub.setPaidAmt(paidAmt);
                BigDecimal chargeAmt = new BigDecimal(loanFin.getActualAmount()).subtract(paidAmt);
                sub.setChargeAmt(chargeAmt);
                sub.setThisRepayAmt(schedule.getRepayAmt());
                sub.setRepayDate(schedule.getRepayDate());
                repaymentScheduleDao.insertSub(sub);
            }
        }
    }
}
